package io.renren.modules.web.token.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.framework.annotation.AuthzCode;
import io.renren.framework.annotation.AuthzRegister;
import io.renren.framework.annotation.Login;
import io.renren.framework.annotation.SysLog;
import io.renren.modules.token.request.InfoRequest;
import io.renren.modules.token.service.ApiTokenInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/token")
@Api(tags = "用户公共接口")
@Slf4j
public class ApiTokenInfoController {
    @Reference
    private ApiTokenInfoService apiTokenInfoService;


    @Login
    @SysLog("登录-信息")
    @PostMapping("info")
    @ApiOperation("登录-信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info",
            cnName = "登录-信息",
            describe = "登录-信息",
            authzCode = "api:token:info"
    )
    @AuthzCode(authzCode = "api:token:info")
    public R info(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            Map map = apiTokenInfoService.info(request);
            log.debug("登录-信息:" + map);
            return R.ok().put("data", map);
        } catch (RRException e) {
            log.debug("异常[api:token:info]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }


    @Login
    @SysLog("登录-信息-用户信息")
    @PostMapping("sysUser")
    @ApiOperation("登录-信息-用户信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_sysUser",
            cnName = "登录-信息-用户信息",
            describe = "登录-信息-用户信息",
            authzCode = "api:token:info:sysUser"
    )
    @AuthzCode(authzCode = "api:token:info:sysUser")
    public R sysUser(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            Map map = apiTokenInfoService.sysUser(request);
            log.debug("登录-信息:" + map);
            return R.ok().put("data", map);
        } catch (RRException e) {
            log.debug("异常[api:token:info:sysUser]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }


    @Login
    @SysLog("登录-信息-角色信息")
    @PostMapping("sysRoles")
    @ApiOperation("登录-信息-角色信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_sysRoles",
            cnName = "登录-信息-角色信息",
            describe = "登录-信息-角色信息",
            authzCode = "api:token:info:sysRoles"
    )
    @AuthzCode(authzCode = "api:token:info:sysRoles")
    public R sysRoles(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            List list = apiTokenInfoService.sysRoles(request);
            log.debug("登录-信息:" + list);
            return R.ok().put("data", list);
        } catch (RRException e) {
            log.debug("异常[api:token:info:sysRoles]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }


    @Login
    @SysLog("登录-信息-服务信息")
    @PostMapping("authzCodes")
    @ApiOperation("登录-信息-服务信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_authzCodes",
            cnName = "登录-信息-服务信息",
            describe = "登录-信息-服务信息",
            authzCode = "api:token:info:authzCodes"
    )
    @AuthzCode(authzCode = "api:token:info:authzCodes")
    public R authzCodes(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            List list = apiTokenInfoService.authzCodes(request);
            log.debug("登录-信息:" + list);
            return R.ok().put("data", list);
        } catch (RRException e) {
            log.debug("异常[api:token:info:authzCodes]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }

    @Login
    @SysLog("登录-信息-基础信息")
    @PostMapping("cuInfo")
    @ApiOperation("登录-信息-基础信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_cuInfo",
            cnName = "登录-信息-基础信息",
            describe = "登录-信息-基础信息",
            authzCode = "api:token:info:cuInfo"
    )
    @AuthzCode(authzCode = "api:token:info:cuInfo")
    public R cuInfo(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            Map map = apiTokenInfoService.cuInfo(request);
            log.debug("登录-信息:" + map);
            return R.ok().put("data", map);
        } catch (RRException e) {
            log.debug("异常[api:token:info:cuInfo]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }

    @Login
    @SysLog("登录-信息-统计信息")
    @PostMapping("cuCount")
    @ApiOperation("登录-信息-统计信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_cuCount",
            cnName = "登录-信息-统计信息",
            describe = "登录-信息-统计信息",
            authzCode = "api:token:info:cuCount"
    )
    @AuthzCode(authzCode = "api:token:info:cuCount")
    public R cuCount(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            Map map = apiTokenInfoService.cuCount(request);
            log.debug("登录-信息:" + map);
            return R.ok().put("data", map);
        } catch (RRException e) {
            log.debug("异常[api:token:info:cuCount]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }

    @Login
    @SysLog("登录-信息-账号信息")
    @PostMapping("cuAccount")
    @ApiOperation("登录-信息-账号信息")
    @ResponseBody
    @AuthzRegister(
            roleName = "SYSTEM",
            authoritiesName = "api_token",
            authoritiesCnName = "系统登录权限",
            name = "api_token_info_cuAccount",
            cnName = "登录-信息-账号信息",
            describe = "登录-信息-账号信息",
            authzCode = "api:token:info:cuAccount"
    )
    @AuthzCode(authzCode = "api:token:info:cuAccount")
    public R cuAccount(@Valid @RequestBody InfoRequest request, BindingResult result) {

        if (result.hasErrors()) {
            return R.onBindingError(result.getAllErrors());
        }
        try {
            Map map = apiTokenInfoService.cuAccount(request);
            log.debug("登录-信息:" + map);
            return R.ok().put("data", map);
        } catch (RRException e) {
            log.debug("异常[api:token:info:cuAccount]:" , e);
            return R.error(e.getCode(), e.getMessage());
        }

    }
}
